package com.jinlejia.bbs.widget.musicView;

import android.app.Activity;
import android.content.Context;
import android.os.Build;

import java.lang.ref.WeakReference;

/**
 * @author 王进 on 2019/4/11.
 */

abstract class Host<T> extends WeakReference<T>{
    Host(T referent) {
        super(referent);
    }

    public abstract Context getContext();
    public abstract boolean isAlive();

    public static final Host create(Context context){
        return new HostContext(context);
    }

    static class HostContext extends Host<Context>{

        public HostContext(Context referent) {
            super(referent);
        }

        @Override
        public Context getContext() {
            return get();
        }

        @Override
        public boolean isAlive() {
            Context context = get();
            if (context==null)return false;
            if (context instanceof Activity){
                Activity activity = (Activity) context;
                if (activity.isFinishing()){
                    return false;
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    if (activity.isDestroyed()){
                        return false;
                    }
                }
            }
            return true;
        }
    }
}
